'''
这个文件不影响网盘，只是为了测试代理重加密算法
'''
from cloudcrypto import *
import pymysql as pm#python连接数据库的库
from clouddatabase import *
from umbral import *
# cloud_pre_genkey()

# 这是一个验证实验
# alice's key
alices_path = './alice'
alices_signing_key, alices_verifying_key = cloud_pre_restore_signingkeypair(alices_path+'/etc')
alices_signer = Signer(alices_signing_key)
alices_secret_key, alices_public_key = cloud_pre_restore_delegatingkeypair(alices_path+'/etc')

# alice's capsule and ciphertext
with open(alices_path+'/share/759fe697492a134bab6557cacc5cf63d779e849c5a1736145a2b0d3ea57317c1', 'rb') as f:
    capsule_bytes = f.read(98)
    print('capsule_bytes', capsule_bytes.hex())
    ciphertext = f.read()
    print('ciphertext', ciphertext.hex())
capsule = Capsule._from_exact_bytes(capsule_bytes)

#cleartext = decrypt_original(alices_secret_key, capsule, ciphertext)
#print(cleartext)

# bob's key
bobs_path = './bob'
bobs_signing_key, bobs_verifying_key = cloud_pre_restore_signingkeypair(bobs_path+'/etc')
bobs_signer = Signer(bobs_signing_key)
bobs_secret_key, bobs_public_key = cloud_pre_restore_delegatingkeypair(bobs_path+'/etc')


# 产生转换密钥过程
kfrags = generate_kfrags(delegating_sk=alices_secret_key,
                         receiving_pk=bobs_public_key,
                         signer=alices_signer,
                         threshold=1,
                         shares=1)
print(len(kfrags))
kfrag = kfrags[0]
kfrag_bytes = kfrag.__bytes__()
print(len(kfrag_bytes))

# with open(alices_path+'/etc/kfrag_bytes.txt', 'wb') as f:
#     f.write(kfrag_bytes)

with open(alices_path+'/etc/kfrag_bytes.txt', 'rb') as f:
    kfrag_bytes = f.read()
kfrag = VerifiedKeyFrag.from_verified_bytes(kfrag_bytes)

cfrag = reencrypt(capsule=capsule, kfrag=kfrag)

cfrags = [cfrag]

# bob解密过程
bob_cleartext = decrypt_reencrypted(receiving_sk=bobs_secret_key,
                                    delegating_pk=alices_public_key,
                                    capsule=capsule,
                                    verified_cfrags=cfrags,
                                    ciphertext=ciphertext)

print(bob_cleartext)

